﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Stanza.Silverlight.Controls
{
    public class NumericRangeTranslation
    {
        private const double Zero = 0.0;
        private readonly double screenMin, screenMax;
        private readonly double valueMin, valueMax;
        private readonly bool reverseScreenDirection;
        private double percentValue;

        public NumericRangeTranslation( double screenMax, double valueMax ) :
            this( screenMax, valueMax, false )
        {
        }

        public NumericRangeTranslation( double screenMax, double valueMax, bool reverseScreenDirection )
        {
            this.screenMin = Zero;
            this.screenMax = screenMax;
            this.valueMin = Zero;
            this.valueMax = valueMax;
            this.reverseScreenDirection = reverseScreenDirection;
        }

        public double ScreenValue
        {
            get
            {
                return ( ( reverseScreenDirection ? ( 1.0 - percentValue ) : percentValue ) * screenMax );
            }
            set
            {
                double screenValue = value;
                if( screenValue < screenMin )
                    screenValue = screenMin;
                else if( screenValue > screenMax )
                    screenValue = screenMax;
                percentValue = screenValue / ( screenMax - screenMin );
                if( reverseScreenDirection )
                    percentValue = 1.0 - percentValue;
            }
        }


        public double ItemValue
        {
            get
            {
                return ( percentValue * valueMax );
            }
            set
            {
                double itemValue = value;
                if( itemValue < valueMin )
                    itemValue = valueMin;
                else if( itemValue > valueMax )
                    itemValue = valueMax;
                percentValue = itemValue / ( valueMax - valueMin );
            }
        }
    }
}
